<template>

  <div>

    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true" v-if="hasAuth('depts:add')">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        stripe
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">

      <el-table-column
          prop="name"
          label="名称"
          sortable
          width="180">
      </el-table-column>

      <el-table-column
          prop="dept_sort"
          label="排序号">
      </el-table-column>
      <el-table-column
          prop="enabled"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.enabled" type="success">正常</el-tag>
          <el-tag size="small" v-else-if="scope.row.enabled" type="danger">禁用</el-tag>
        </template>

      </el-table-column>

      <el-table-column
          prop="create_time"
          width="200"
          label="创建时间"
      >
      </el-table-column>
      <el-table-column
          prop="icon"
          label="操作">

        <template slot-scope="scope">
          <el-button type="text" @click="editHandle(scope.row.id)" v-if="hasAuth('depts:update')">编辑</el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)" v-if="hasAuth('depts:delete')">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>


    </el-table>


    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">


        <el-form-item label="部门名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门序号" prop="dept_sort" label-width="100px">
          <el-input v-model="editForm.dept_sort" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="enabled" label-width="100px">
          <el-radio-group v-model="editForm.enabled">
            <el-radio :label='false'>禁用</el-radio>
            <el-radio :label='true'>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="顶级部门" prop="pid_id" label-width="100px">
          <el-radio-group v-model="editForm.type"
                          @input="editForm.type==1?chooseParentDept=true:chooseParentDept=false">
            <el-radio :label=0>是</el-radio>
            <el-radio :label=1>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级部门" prop="pid_id" v-show="chooseParentDept">
          <el-select v-model="editForm.pid_id" placeholder="请选择上级部门">
            <template v-for="item in tableData">
              <el-option :label="item.name" :value="item.id"></el-option>
              <template v-for="child in item.children">
                <el-option :label="child.name" :value="child.id">
                  <span>{{ "- " + child.name }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Dept",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      dialogVisible: false,
      editForm: {},

      tableData: [],

      editFormRules: {
        name: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入唯一编码', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },

      multipleSelection: [],

      permDialogVisible: false,
      permForm: {},
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permTreeData: [],
      // 新增部门，上级部门是否显示
      chooseParentDept: false

    }
  },
  created() {
    this.getDeptList()

  },
  methods: {

    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },

    getDeptList() {
      this.$http.get("/permission/dept/").then(res => {
        this.tableData = res.data.results
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let post_type=this.editForm.id ? this.$http.put : this.$http.post
          post_type('/permission/dept/'+ (this.editForm.id ? this.editForm.id+'/' : ''), this.editForm)
              .then(res => {
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose: () => {
                    this.getDeptList()
                  }
                });

                this.dialogVisible = false
                this.resetForm(formName)
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 删除功能
    delHandle(id) {
      this.$http.delete("/permission/dept/" + id + "/").then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getDeptList()
          }
        });

      })
    },
    // 修改功能
    editHandle(id) {
      this.$http.get('/permission/dept/' + id+'/').then(res => {
        this.editForm = res.data
        console.log(res.data)

        this.dialogVisible = true
      })
    },

  }
}
</script>

<style scoped>

</style>